export default {
  data() {
    return {
      Value: ''
    }
  },
  computed: {
    maxHeight() {
      return this.$store.state.maxHeight
    }
  },
  methods: {
    search() {
      const reg = /^[0-9]*[1-9][0-9]*$/;
      this.Value = this.Value.trim();
      if (this.Value.trim() == "")
        return this.$toast(this.$t('home.keyword'));
      if (reg.test(this.Value.trim()) || this.Value == 0) {
        if (Number(this.Value.trim()) <= Number(this.maxHeight)) {
          //判断两个高度差---int类型比较
          this.Rpc.getBlockHash(Number(this.Value)).then(data => {
            if (data.error == null) {
              this.$router.push({
                path: "/blockDetail",
                query: {
                  height: this.Value.trim()
                }
              });
              this.Value = "";
            } else {
              this.$toast(this.$t('components.errMsg'));
              this.Value = "";
            }
          });
        } else {
          this.$toast(this.$t('components.errHeight'));
          this.Value = "";
        }
      } else {
        let e = this.Value.length;
        if (e === 66 || e === 64) {
          this.Value = e === 64 ? "0x" + this.Value : this.Value;
          //交易的哈希值---地址64位字节加上16进制标示0x(共66位)
          this.Rpc.queryTransaction(this.Value).then(data => {
            if (data.error == null) {
              this.$router.push({
                path: "/tradeHash",
                query: {
                  hash: this.Value
                }
              });
              this.Value = "";
            } else {
              //区块哈希
              this.Rpc.getBlockOverview(this.Value).then(data => {
                if (data.error == null) {
                  this.$router.push({
                    path: "/blockDetail",
                    query: {
                      height: data.result.head.height
                    }
                  });
                  this.Value = "";
                } else {
                  this.$toast(this.$t('components.errMsg'));
                  this.Value = "";
                }
              });
            }
          });
        } else {
          //地址
          this.Rpc.getAddrOverview(this.Value).then(data => {
            if (data.error == null) {
              this.$router.push({
                path: "/address",
                query: {
                  address: this.Value,
                  way: 0
                }
              });
              this.Value = "";
            } else {
              this.$toast(this.$t('components.errMsg'));
              this.Value = "";
            }
          });
        }
      }
    }
  }
}